<?php
class Index extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'commonClass' );
		$this->load->library ( 'page' );
		session_start();
	}
	function index() {
		$lang ['error'] = '';
		// 检测用户是否登入
		$uid = commonClass::Listion_user ();
		if ($uid) {
			$lang ['uid'] = $_SESSION ['uid'];
			$lang ['usermail'] = $_SESSION ['usermail'];
			$lang ['login_from'] = isset ( $_SESSION ['login_from'] ) ? $_SESSION ['login_from'] : "";
			$lang ['email'] = isset ( $_SESSION ['email'] ) ? $_SESSION ['email'] : "";
			$where ["id"] = $uid;
			$re_user = $this->m_tbk->getOne ( "user", $where );
			$lang ["tao_user"] = $re_user ["tao_user"];
			$lang ["fanli"] = empty ( $re_user ["fanli"] ) ? '0.00' : $re_user ['fanli'];
			$lang ["is_fanli"] = $re_user ["is_fanli"];
		
		} else {
			echo "error,<a href=" . site_url ( 'tbk' ) . ">back</a>";
			die ();
		}
		$siz = 10; // 每页10个
		$sql = "select * from tb_tbk_report where outer_code=$uid GROUP BY trade_parent_id ";
		$count = $this->m_tbk->count ( $sql );
		$t = new Page($count,$siz);
		$page =  $t->show();
		$sql = "select trade_parent_id,pay_time from tb_tbk_report where outer_code=" . $uid . " GROUP BY trade_parent_id LIMIT " . $t->firstRow . " , " .$t->listRows;
		$trade_parent_id = $this->m_tbk->tbk_query ( $sql );
		$result_list = array ();
		if ($trade_parent_id) {
			foreach ( $trade_parent_id as $vk ) {
				$sql = "select item_title,real_pay_fee,item_num,commission,is_fanli from tb_tbk_report where outer_code=" . $uid . " and trade_parent_id =" . $vk->trade_parent_id;
				unset ( $re );
				$re = $this->m_tbk->tbk_query ( $sql );
				$result_list [$vk->trade_parent_id] ["pay_time"] = $vk->pay_time;
				$result_list [$vk->trade_parent_id] ["detail"] = $re;
				$result_list [$vk->trade_parent_id] ["pay_sum"] = 0;
				$result_list [$vk->trade_parent_id] ["commission_sum"] = 0;
				$result_list [$vk->trade_parent_id] ["num"] = 0;
				foreach ( $re as $vb ) {
					$result_list [$vk->trade_parent_id] ["num"] ++;
					$result_list [$vk->trade_parent_id] ["pay_sum"] += $vb->real_pay_fee;
					$result_list [$vk->trade_parent_id] ["commission_sum"] += $vb->commission;
				}
			}
		}
		
		// 提取已提交的金额
		$res = $this->m_tbk->in_rebate ( $lang ['uid'] );
		if ($res) {
			$lang ['rebate_cash'] = $res [0] ['rebate_cash'];
		} else {
			$lang ['rebate_cash'] = '0.00';
		}
		
		$lang ["table"] = $result_list;
		$lang ["page"] = $page;
		$this->load->view ( "user/v_user", $lang );
	}
	function taobao_add() {
		if ($_POST ["tao_user"] == "") {
			echo false;
			return;
		}
		$data ["tao_user"] = $_POST ["tao_user"];
		$where ["id"] = $_SESSION ["uid"];
		if ($this->m_tbk->update ( "user", $data, $where )) {
			echo true;
		} else {
			echo false;
		}
	}
	
	function rebate_add() {
		if (! isset ( $_SESSION ["uid"] )) {
			echo "error";
			die ();
		}
		
		$data ['uip'] = $_SESSION["ip"];
		$data ['uid'] = $_SESSION ['uid'];               //会员id
		$data ['rebate_cash'] = $this->input->post ( 'fanli' ); //申请金额
		$data ['tao_user'] = $this->input->post ( 'tao_user' );  //淘宝账号
		$data ['submit_date'] = time();         //提交时间 保存时间戳
		$res = $this->m_tbk->rebate_add ("rebate", $data );
		if ($res) {
			$lang ['error'] = '';
			$lang ["waitSecond"] = 1;
			$lang ["jumpUrl"] = site_url("user/index/");
			$lang ["msg"] = "提交成功！";
			$this->load->view ( 'public/v_success', $lang );
		} else {
			$lang ['error'] = '';
			$lang ["waitSecond"] = 5;
			$lang ["jumpUrl"] = site_url("user/index/");
			$lang ["msg"] = "提交失败！";
			$this->load->view ( 'public/v_error', $lang );
		}
	}
	function check_num() {
		if (! isset ( $_SESSION ["uid"] )) {
			echo "error";
			die ();
			return;
		}
		$uid = $_SESSION ["uid"];
		$num = $_POST ["num"];
		$where ["id"] = $uid;
		$re = $this->m_tbk->getOne ( "user", $where );
		if (($re ["fanli"] - $re ["is_fanli"]) >= $num) {
			echo 1;
		} else
			echo 0;
	}
	function email_update() {
		$data ['email'] = $this->input->post ( 'email' );
		$data ['user_id'] = $_SESSION ['uid'];
		if (preg_match ( "/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/", $data['email'] )) {
			$res = $this->m_tbk->email_update ( $data );
			if($res){
				$_SESSION['email'] = $data['email'];
				echo '操作成功<a href='.site_url("user/index/").'>返回</a>';
			}else{
				echo '操作失败<a href='.site_url("user/index/").'>返回</a>';
			}
		}else{
			echo '邮箱不正确<a href='.site_url("user/index/").'>返回</a>';
		}
	}
}